/*
 Copyright 2007 Razvan Taranu

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */
package generators;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.thoughtworks.qdox.model.BeanProperty;
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.Type;

public class JavaJsonizers {
    private final Log log = LogFactory.getLog(JavaJsonizers.class);

    private static final String classTemplate;
    static {
        classTemplate = Utils.readRessource("templates/java/jsonizer/BeanJsonizer.tpl");
    }

    private String beansPackage;

    private String jsonizersPackage;

    public void run() {
        log.info("generating JAVA jsonizers");

        for (BeanInfo bean : Beans.beanList) {
            log.info("processing " + bean.getJavaSource().getURL());

            String content = generate(bean);

            JavaHelper.writeJsonizer(bean.getJavaClass().getName(), content);
        }

        JavaHelper.copyJsonizers();
    }

    private String generate(BeanInfo bean) {
        JavaClass javaClass = bean.getJavaClass();

        List<String> toJava = new ArrayList<String>();
        List<String> toJson = new ArrayList<String>();

        for (BeanProperty prop : bean.getProperties()) {
            toJava.add(toJava(prop));
            toJson.add(toJson(prop));
        }

        String output = classTemplate;

        String className = javaClass.getFullyQualifiedName();
        String shortClassName = javaClass.getName();

        String beanGetters = StringUtils.join(toJson, "\n            ");
        String beanSetters = StringUtils.join(toJava, "\n            ");

        output = output.replace("@@outputPackage@@", jsonizersPackage);
        output = output.replace("@@sourcePackage@@", beansPackage);

        output = output.replace("@@className@@", className);
        output = output.replace("@@shortClassName@@", shortClassName);

        output = output.replace("@@beanGetters@@", beanGetters);
        output = output.replace("@@beanSetters@@", beanSetters);

        return output;
    }

    private String toJava(BeanProperty property) {
        String value = "jsonObject.get(\"" + property.getName() + "\")";
        Type type = property.getType();

        String jsonizer = JavaHelper.valueToJava(value, type);

        // jsonizer = ObjectJsonizer.toJsonString(arg0.get("string"))

        String objectSet = "toReturn.@@mutator@@(@@jsonizer@@);";

        String mutator = property.getMutator().getName();

        objectSet = objectSet.replace("@@mutator@@", mutator);
        objectSet = objectSet.replace("@@jsonizer@@", jsonizer);

        return objectSet;

    }

    private String toJson(BeanProperty property) {
        String value = "arg0." + property.getAccessor().getName() + "()";
        Type type = property.getType();

        String jsonizer = JavaHelper.valueToJson(value, type);

        // jsonizer = ObjectJsonizer.toJsonString(arg0.getString())

        String objectSet = "json.put(\"@@propName@@\", @@jsonizer@@);";

        String propName = property.getName();

        objectSet = objectSet.replace("@@propName@@", propName);
        objectSet = objectSet.replace("@@jsonizer@@", jsonizer);

        return objectSet;
    }

    public void setBeansPackage(String beansPackage) {
        this.beansPackage = beansPackage;
    }

    public void setJsonizersPackage(String jsonizersPackage) {
        this.jsonizersPackage = jsonizersPackage;
    }
}
